from tortoise import fields, models
from tortoise.contrib.pydantic import pydantic_model_creator


class SonarProfile(models.Model):
    Profile_Name = fields.CharField(max_length=222)
    Freq_1 = fields.SmallIntField()
    Freq_2 = fields.SmallIntField()
    Freq_3 = fields.SmallIntField()
    Freq_4 = fields.SmallIntField()
    Freq_5 = fields.SmallIntField()
    TPK = fields.SmallIntField()
    Blades = fields.SmallIntField()
    Test = fields.SmallIntField(default=10)

    # def __str__(self):
    #     return self.Profile_Name

    class Meta:
        table = "restAPI_sonarprofile"


SonarProfile_Pydantic = pydantic_model_creator(SonarProfile, name="SonarProfile")
SonarProfileIn_Pydantic = pydantic_model_creator(SonarProfile, name="SonarProfileIn", exclude_readonly=True)


class User(models.Model):
    """
    The User model
    """

    id = fields.IntField(pk=True, index=True)
    username = fields.CharField(max_length=20, unique=True, null=False)
    # email = fields.CharField(max_length=20, unique=True, null=False)
    email = fields.CharField(max_length=20,  null=False)
    password = fields.CharField(max_length=228, null=False)
    is_verified = fields.BooleanField(default=False)
    join_date = fields.DatetimeField(auto_now=True)


class Business(models.Model):
    id = fields.IntField(pk=True, index=True)
    business_name = fields.CharField(max_length=20, null=False, unique=True)
    city = fields.CharField(max_length=100, null=False, default="Unspecified")
    region = fields.CharField(max_length=100, null=False, default="Unspecified")
    business_description = fields.TextField(null=True)
    logo = fields.CharField(max_length=200, null=False, default='default.jpg')
    owner = fields.ForeignKeyField('models.User', related_name='business')


class Product(models.Model):
    id = fields.IntField(pk=True, index=True)
    name = fields.CharField(max_length=20, null=False, unique=True)
    category = fields.CharField(max_length=100, null=False, default="Unspecified")
    original_price = fields.DecimalField(max_digits=12, decimal_places=2)
    new_price = fields.DecimalField(max_digits=12, decimal_places=2)
    percent_discount = fields.IntField()
    offer_expiration_data = fields.DatetimeField(auto_now=True)
    product_image = fields.CharField(max_length=200, null=False, default='product.jpg')
    business = fields.ForeignKeyField('models.Business', related_name='products')


user_pydantic = pydantic_model_creator(User, name='User', exclude=('is_verified',))
user_pydanticIn = pydantic_model_creator(User, name='UserIn', exclude_readonly=True, exclude=('is_verified', 'join_date'))
user_pydanticOut = pydantic_model_creator(User, name='UserOut', include=('business', 'username', 'id', 'email', 'join_date',), exclude=('password', 'is_verified', ))

business_pydantic = pydantic_model_creator(Business, name='Business')
business_pydanticIn = pydantic_model_creator(Business, name='BusinessIn', exclude_readonly=True)

product_pydantic = pydantic_model_creator(Product, name='Product')
product_pydanticIn = pydantic_model_creator(Product, name='ProductIn', exclude=('percent_discount', 'id'))
